<?php
/**
 * the event handler!
 * use simple functions to solve big problems!
 */
class Magnum_Handlers_Base
{
    /**
     * control
     * @var Magnum_Control
     */
    protected $_control = null;


    // get or post?
    protected $_type = 'GET';


    /**
     * view
     * @var Magnum_View
     */
    protected $_view = null;


    // initialise the thing
    public function __construct(Magnum_Control $control)
    {
        $this->_log('Start Event handling', 'constructor');
        // control
        $this->_control = $control;

        // view
        $this->_view = $control->getView();

        // do init
        $this->_init();

        // continue the flow
        // use request to get all stuff
        $requests = $control->getRequest();

        // check if function exists...
        $callback = strtolower($requests[0]);
        $this->_log('Get request', 'constructor', $requests);

        // rest will be params...
        $params = $requests;
        unset($params[0]);

        // do params together with post/get...
        $params = array('_TRIGGER' => $params);

        // if get...
        if ($this->_type == 'get') {
            $params['_GET'] = $_GET;
        } else {
            $params['_POST'] = $_POST;
        }

        // if exists...
        if (method_exists($this, $callback)) {
            $this->_log('Trigger predefined event handling function', $callback, $params);
            return $this->{$callback}($params);
        }

        // for get only:
        if ($this->_type == 'get') {
            // log when error
            $this->_log('Trigger predefined event handling function failed...', $callback, $params, 1);

            // otherwise... go to default... for the last request, need an html page for it...
            $req = implode('_', $requests);

            // try to find it in views...
            $this->_log('Try to render default page', 'renderFile', $req . '.php');
            $control->getView()->addTemplate('body', $req . '.php');
        }

    }// end __construct


    // abstract init...
    protected function _init()
    {

    }// abstract...


    /**
     * log the action
     */
    protected function _log($message, $func = 'N/A', $params = array(), $isErr = false)
    {
        if (!defined('_LOG') || !_LOG) {
            return $this;
        }
        // construct the log from here...
        $details = array(
            'class' => $this->_type,
            'func' => $func,
            'params' => $params
        );

        __log($message, 'event_handler_' . $this->_type, $details, $isErr);
        return $this;


    }// end _log
}
?>